//
//  HWHaveToBuyViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/20.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWHaveToBuyViewController.h"
#import "HWAlltrideJiaoyiCell.h"
#import "HWPurchaseOrderModel.h"
#import "HWAccountDetailsViewController.h" // 详情
@interface HWHaveToBuyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@end

@implementation HWHaveToBuyViewController

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-40-NavBarHeight) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = RGB(244, 243, 244);
        
        
    }
    return   _mainaTableVie ;
}






- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
}



// 设置UI
- (void)setupUI {
    
    self.navigationItem.title = @"我的交易";
    self.view.backgroundColor = [UIColor orangeColor];
    
    [self.view addSubview:self.mainaTableVie];
    
    [self addrefreshControl];
}




#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
    
    
    self.p = @"1";
    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    int t =  [WMUtil getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"4"];
    
    
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];
    
    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
        @"p" :self.p,
        @"ios" : @"1",
        @"ct" :  @"app",
        @"ac" : @"trade_my",
        @"uid" :uidStr,
        @"t" : tStr,
        @"type" :typeStr,
        @"sign" : singMD5
    };
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            
            self.dataMuArr = [HWPurchaseOrderModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            
            
            // [self.mainaTableVie reloadData];
            
            
        }
        [self.mainaTableVie reloadData];
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
    
    int p = [self.p intValue];
    
    int max_p = [self.max_p intValue];
    
    if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
        
        return ;
        
    }
    
    
    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    
    int t =  [WMUtil getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"4"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];
    
    
    NSDictionary *parametes = @{
        @"p" :self.p,
        @"ios" : @"1",
        @"ct" :  @"app",
        @"ac" : @"trade_my",
        @"uid" :uidStr,
        @"t" : tStr,
        @"type" :typeStr,
        @"sign" : singMD5
    };
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            
            
            NSMutableArray *mutableArr = [HWPurchaseOrderModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            for (int i = 0; i < mutableArr.count; i++) {
                [self.dataMuArr addObject:mutableArr[i]];
                
            }
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            
            
        }
        [self.mainaTableVie reloadData];
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
    
}








- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.dataMuArr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    
    HWAlltrideJiaoyiCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWAlltrideJiaoyiCell" owner:nil options:nil].firstObject;
    }
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HWPurchaseOrderModel *model = self.dataMuArr[indexPath.row];
    cell.datamdoel = model;
    [cell.shanchudingdanBut  addTarget:self action:@selector(shanchuding:) forControlEvents:UIControlEventTouchUpInside];
    
    return  cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  36+70+36+10;
    
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWPurchaseOrderModel *model = self.dataMuArr[indexPath.row];
    
    HWAccountDetailsViewController *accountDetails = [[HWAccountDetailsViewController alloc]init];
    
    accountDetails.idStr = model.id;
    [self.navigationController pushViewController:accountDetails animated:YES];
    
    
    
}




#pragma mark ------------- 删除订单

- (void)shanchuding:(UIButton *)sender {
    NSLog(@"cell ------ %@",[[[sender superview] superview] superview]);
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认删除？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        HWAlltrideJiaoyiCell *cell = (HWAlltrideJiaoyiCell *)[[[sender superview] superview] superview];
        NSIndexPath *index = [self.mainaTableVie indexPathForCell:cell];
        
        HWPurchaseOrderModel *model = self.dataMuArr[index.row];
        
        
        
        NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
        int t =  [WMUtil getRandomNumber:101 to:998];
        NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
        
        
        NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
        
        NSString *singMD5 = [WMUtil md5HexDigest:singStr];
        
        self.dataMuArr = [[NSMutableArray alloc]init];
        NSDictionary *parametes = @{
            
            @"ios" : @"1",
            @"ct" :  @"app",
            @"ac" : @"trade_my_del",
            @"uid" :uidStr,
            @"t" : tStr,
            @"id" : model.id,
            @"sign" : singMD5
        };
        
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
            
            
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            if ([codeStr intValue] == 1) {
                
                
                [self.mainaTableVie.mj_header beginRefreshing];
                
                
                
            } else {
                
                [MBProgressHUD showError:@"删除订单失败"];
                
            }
            
            
            
        } failure:^(NSString *error) {
            
            
            [MBProgressHUD showError:@"删除订单失败"];
            
        }];
        
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}







@end
